<?php

class AudioController extends FrontEndController
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('index', 'view', 'create', 'update', 'upload', 'uploadpopup', 'deleteAudio', 'copy'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionIndex()
	{
		$user_id = isset($_GET['profile']) ? $_GET['profile']: Yii::app()->user->id;
		$dataProvider = MusicUser::model()->findAll(
			'user_id=:user_id AND status=1',
			array(':user_id'=>$user_id)
		);
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}
	public function actionUpload(){
		$model=new Music;

		if(Yii::app()->request->getParam('profile')!=NULL)
		{
			$model->attributes = array('file'=>'');
			$path = $_SERVER['DOCUMENT_ROOT'].'/upload/audio/'.Yii::app()->request->getParam('id').'/';
			
			$model->file = self::saveMusic($_FILES['image'], $path);
			$model->name = '';
			$model->artist = '';	
			$model->save();
			echo "{";
			echo		"msg: '" . $model->id . "',\n";
			echo		"music_name: '" . $_FILES['image']['name'] . "'\n";
			echo "}";
		}
	}
	public function actionUploadPopup(){
		$model=new Music;
		$model->attributes = array('file'=>'');
		$path = $_SERVER['DOCUMENT_ROOT'].'/upload/audio/'.Yii::app()->request->getParam('id').'/';
		$model->file = self::saveMusic($_FILES['image'], $path);
		$model->name = $_FILES['image']['name'];
		$model->artist = '';
		$model->save();
		
		$make = new MusicUser;
		$make->music_id = $model->id;
		$make->user_id = Yii::app()->user->id;
		$make->artist = '';
		$make->name = $_FILES['image']['name'];
		$make->crdate = date("Y-m-d H:i:s");
		$make->status = 0;
		$make->save();		

		echo "{";
		echo				"msg: '" . $make->id . "',\n";
		echo				"file: '" . self::saveMusic($_FILES['image'], $path) . "',\n";
		echo				"name: '" . $_FILES['image']['name'] . "'\n";
		echo "}";			
	}	
	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model = new Music;
		
		if(isset($_POST['Music']))
		{
			$_POST['Music']['user_id'] = Yii::app()->user->id;
			$model=$this->loadModel($_POST['Music']['id']);
			$path = $_SERVER['DOCUMENT_ROOT'].'/upload/audio/';
			$model->attributes=$_POST['Music'];
			$model->save();
			//music user
			$make = new MusicUser;
			$make->music_id = $model->id;
			$make->user_id = Yii::app()->user->id;
			$make->artist = $_POST['Music']['artist'];
			$make->name = ($_POST['Music']['name']) ? $_POST['Music']['name'] : 'undefined';
			$make->crdate = date("Y-m-d H:i:s");
			$make->status = 1;
			$make->save();
			$this->actionIndex();
		} else {
			$this->render('create',array(
				'model'=>$model,
			));
		}
	}
	public function actionCopy($id)
	{
		if(isset($_GET['id']))
		{
			if(isset($_GET['main'])){
				$model=$this->loadModel($_GET['id']);
			} else {
				$model=MusicUser::model()->findByPk($_GET['id']);
			}
			//music user
			$make = new MusicUser;
			$make->music_id = $_GET['id'];
			$make->user_id = Yii::app()->user->id;
			$make->artist = $model->artist;
			$make->name = $model->name;
			$make->crdate = date("Y-m-d H:i:s");
			$make->status = 1;
			$make->save();
		}
	}
	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=MusicUser::model()->findByPk($id);
		
		if(isset($_POST['MusicUser']))
		{
			$model->attributes=$_POST['MusicUser'];
			if($model->save())
				//$this->redirect(array('index','id'=>$model->id));
				$this->redirect(Yii::app()->createUrl('audio/index', array('profile'=>Yii::app()->request->getParam('profile'))));
		}
		
		$this->render('update',array(
				'model'=>$model,
		));
	}
	
	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();
		
		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}
	public function actionDeleteAudio($id)
	{
		$model = MusicUser::model()->findByPk($id);
		$model->status = 0;
		$model->save();
	}
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Music::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
}
